apiready = function() {
seajs.use(["../../api", "../../jquery", "../../app",'../../service'], function($api, $, app,service) {
    var vm = {
        el: '#app',
        data: {
          goods_number:3,
          isShop_user:0,
          shopData:{},
          vipGoodsList:[],
          goodsList:[],
          isCollect:0,
          classifys:[]
        },
        mounted: function() {
          var that = this
            this.init()
            // app.refreshHeaderInfo(function() {
            //     that.init();
            // });
        },
        methods: {
            init: function() {
                var that = this;
                this.Shop_get(function(){
                  that.searchGoods(0)//普通商品
                  that.searchGoods(1)//会员商品
                  that.checkIsCollect()//检测是否收藏当前店铺
                  that.getclassifyList()
                })
                this.$nextTick(function(){
                  $('.con_').slideDown()
                  setTimeout(function(){
                    $('.vip_goods_box_').css('width','calc((45vw + 5px)*'+that.vipGoodsList.length+')')
                  },500)
                })
            },
            // 前往升星
            toStar:function(){
              api.openWin({
                  name: 'index',
                  url: 'widget://src/Shop/shop_index/shop_index.html',
                  pageParam: {},
                  slidBackEnabled: false
              })
            },
            toAddMember: function(goods_id){
              app.openWin('shopEditMember',{shopId: this.shopData.shop_code}, {reload: true})
            },
            // 前往分类
            toClass:function(category_id,name){
              api.openWin({
                  name: 'classify',
                  url: './classify.html',
                  pageParam: {
                      shopId: $api.getStorage('user').data.shop_code,
                      cat_id: category_id,
                      name:name
                  }
              });
              $('.shop_banner_head_right_classify').slideUp(200)
            },
            toGoods_detail:function(goods_id,is_vip){
              api.openWin({
                  name: 'goods_detail',
                  url: '../buy_goods/goods.html',
                  pageParam: {
                      goodsId: goods_id,
                      isSeller:is_vip
                  },
                  reload:true
              });
            },
            // 分享店铺
            shareShop:function(){
              var wx = api.require('wx');
                wx.shareWebpage({
                  scene: 'session',
                  title: this.shopData.shop_name+',这个店铺的里的东西快抢疯了',
                  description: '有张订单教你什么叫轻松赚钱',
                  thumb: 'widget://image/logo.png',
                  contentUrl: 'http://apicloud.com'
                }, function(ret, err) {
                  if (ret.status) {
                    api.toast({
                        msg: '分享成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                  }
                });
            },
            // 获取所有分类
            getclassifyList:function(){
              var that = this
              app.ajax(
                  service['getclassifyList'], {
                      data: {
                          values: {
                            shop_code:this.shopData.shop_code,
                            unionid:this.shopData.unionid
                          }
                      }
                  },
                  function(res) {
                      if (res.code == 0) {
                        that.classifys = res.data
                      }
                  })
            },
            // 收藏
            Collect:function(){
              if(this.isCollect==0){//未收藏
                this.addToFavorite()
              }else{
                this.cancelCollect()
              }
            },
            // 添加收藏
            addToFavorite:function(){
              var that = this
              app.ajax(
                  service['addToFavoriteShop'], {
                      data: {
                          values: {
                            shop_id:this.shopData.shop_id,
                            type:2
                          }
                      }
                  },
                  function(res) {
                      if (res.code == 0) {
                      that.isCollect = res.data
                      api.sendEvent({
                          name: 'setcollect'
                      });
                      }
                  })
            },
            // 取消收藏
            cancelCollect:function(){
              var that = this
              app.ajax(
                  service['cancelCollectShop'], {
                      data: {
                          values: {
                            favorite_id:this.isCollect,
                            type:2
                          }
                      }
                  },
                  function(res) {
                      if (res.code == 0) {
                        that.isCollect = res.data
                        api.sendEvent({
                            name: 'setcollect'
                        });
                      }
                  })
            },
            // 检测是否收藏了当前店铺
            checkIsCollect:function(){
              var that = this
              app.ajax(
                  service['checkIsCollect'], {
                      data: {
                          values: {
                            shop_id:this.shopData.shop_id,
                            type:2
                          }
                      }
                  },
                  function(res) {
                      if (res.code == 0) {
                        that.isCollect = res.data
                      }
                  })
            },
            // 获取商品列表
            searchGoods:function(is_member_product){
              var that = this
              app.ajax(
                  service['searchGoods'], {
                      data: {
                          values: {
                            shop_code:this.shopData.shop_code,
                            is_member_product:is_member_product,
                            unionid:this.shopData.unionid
                          }
                      }
                  },
                  function(res) {
                      if (res.code == 0) {
                        if(is_member_product==0){//普通商品
                          that.goodsList = res.data
                        }else{
                          that.vipGoodsList = res.data
                        }
                      }
                  })
            },
            // 获取店铺信息
            Shop_get:function(cb){
              var that = this
              app.ajax(
                  service['Shop_get'], {
                      data: {
                          values: {
                            userid:api.pageParam.out_user_id
                          }
                      }
                  },
                  function(res) {
                      if (res.code == 0) {
                        that.shopData = res.data
                        if(res.data.banner!=''){
                          that.shopData.bannerImage = res.data.banner.url
                        }
                        if(res.data.shop_code==$api.getStorage('user').data.shop_code){
                          that.isShop_user = 1
                        }
                        cb()
                      }
                  })
            },
            search:function(){
              app.openWin('searchDetail', {searchVal: "",shopData:this.shopData});
              // api.openWin({
              //     name: 'search',
              //     url: '../search/search.html',
              //     pageParam:{
              //      shopData:this.shopData
              //     }
              // });
            },
            // 显示分类
            showClassify:function(){
              $('.shop_banner_head_right_classify').slideDown(200)
            },
            // 隐藏分类
            hideClassify:function(){
            $('.shop_banner_head_right_classify').slideUp(200)
            },
            //前往购物车
            toCart: function(){
              app.openWin('buyCart',{},{reload: true})
            },
            // 回到顶部
            toHead:function(){
              api.pageUp({
                  top: true
              })
            }
        }
    }
    var even = function() {

    }
    var newVue = new Vue(vm);
    even();
});

}
